package com.heart.house.api.controller;


import com.heart.house.api.common.ResultMsg;
import com.heart.house.api.common.UserContext;
import com.heart.house.api.model.Agency;
import com.heart.house.api.model.User;
import com.heart.house.api.service.AccountService;
import com.heart.house.api.utils.HashUtils;
import com.heart.house.api.validation.UserHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private AccountService accountService;

    /**
     * 注册提交：1注册验证 2发送邮件 3验证失败重定向到注册页面
     * 注册也获取 根据account对象为依据判断是否注册页获取请求
     *
     * @param account
     * @param modelMap
     * @return
     */
    @RequestMapping("accounts/register")
    public String accountsRegister(User account, ModelMap modelMap) {
        if (account == null || account.getName() == null) {
            return "/user/accounts/register";
        }

        // 用户验证
        ResultMsg resultMsg = userHelper.validate(account);
        if (resultMsg.isSuccess()) {
            boolean exist = accountService.isExist(account.getEmail());
            if (!exist) {
                accountService.addAccount(account);
                modelMap.put("email", account.getEmail());
                return "/user/accounts/registerSubmit";
            } else {
                return "redirect:/accounts/register?" + resultMsg.asUrlParams();
            }
        } else {
            return "redirect:/accounts/register?" + resultMsg.asUrlParams();
        }
    }

    /**
     * 激活邮箱
     *
     * @param key
     * @return
     */
    @RequestMapping("accounts/verify")
    public String verify(String key) {
        boolean result = accountService.enable(key);
        if (result) {
            return "redirect:/index?" + ResultMsg.successMsg("激活成功").asUrlParams();
        } else {
            return "redirect:/accounts/register?" + ResultMsg.errorMsg("激活失败,请确认是否过期").asUrlParams();
        }
    }

    //-----------------------------登陆流程-----------------------

    /**
     * 登陆
     *
     * @param req
     * @return
     */
    @RequestMapping("accounts/signin")
    public String signin(HttpServletRequest req) {
        String username = req.getParameter("username");
        String passwd = req.getParameter("password");
        String target = req.getParameter("target");
        if (username == null || passwd == null) {
            req.setAttribute("target", target);
            return "/user/accounts/signin";
        }
        User user = accountService.auth(username, passwd);
        if (user == null) {
            return "redirect:/accounts/signin?" + "target=" + target + "&username=" + username + "&" + ResultMsg.errorMsg("用户名或密码错误").asUrlParams();
        } else {
            UserContext.setUser(user);
            return StringUtils.isNotBlank(target) ? "redirect:" + target : "redirect:/index";
        }
    }

    /**
     * 退出
     *
     * @param req
     * @return
     */
    @RequestMapping("accounts/logout")
    public String logout(HttpServletRequest req) {
        User user = UserContext.getUser();
        accountService.logout(user.getToken());
        return "redirect:/index";
    }

    //-----------------------------用户个人-----------------------

    /**
     * 个人信息页面
     *
     * @param modelMap
     * @return
     */
    @RequestMapping("accounts/profile")
    public String profile(ModelMap modelMap) {
        List<Agency> list = accountService.getAllAgency();
        modelMap.addAttribute("agencyList", list);
        return "/user/accounts/profile";
    }

    /**
     * 个人页面 更新
     *
     * @param req
     * @param updateUser
     * @param modelMap
     * @return
     */
    @RequestMapping("accounts/changePassword")
    public String changePassword(HttpServletRequest req, User updateUser, ModelMap modelMap) {
        if (updateUser.getEmail()) {
            return "redirect:/accounts/profile?" + ResultMsg.errorMsg("邮箱不能为空").asUrlParams();
        }
        User user = accountService.updateUser(updateUser);
        UserContext.setUser(user);
        return "redirect:/accounts/profile?" + ResultMsg.successMsg("更新成功").asUrlParams();
    }

}

